package service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.ComplaintAuditDao;
import dao.MerchantAuditDao;
import dao.impl.MerchantAuditDaoImpl;
import po.ComplaintAudit;
import po.MerchantAudit;
import service.ComplaintAuditManager;
import service.WebService;
import vo.OrderComplaintVo;
import vo.Orders;

@Service("complaintAuditManager")
public class ComplaintAuditManagerImpl implements ComplaintAuditManager {

	@Autowired
	private ComplaintAuditDao cad;
	@Autowired
	private MerchantAuditDao mad;
	@Autowired
	private WebService ws;
	
	@Override
	public String addComplaint(ComplaintAudit complaint) {
		// TODO Auto-generated method stub
		return cad.inserOrderComplaint(complaint);
	}

	@Override
	public List<OrderComplaintVo> listAllComplaint() {
		// TODO Auto-generated method stub
		List<ComplaintAudit> listAudit =  cad.listAllOrderComplaint();
		List<OrderComplaintVo> listComplaint = new ArrayList<OrderComplaintVo>();
		List<String> list = new ArrayList<String>();
		for (ComplaintAudit complaintAudit : listAudit) {
			list.add(complaintAudit.getoId());
		}
		Map<String,Orders> orderMap= ws.listAllOrders(list);
		for (ComplaintAudit complaintAudit_ : listAudit) {
			listComplaint.add(new OrderComplaintVo(orderMap.get(complaintAudit_.getoId()),
					complaintAudit_.getCreateDate(), complaintAudit_.getStatus()));
		}
		return listComplaint;
		
	}

	@Override
	public String auditComplaint(String oid, int status) {
		// TODO Auto-generated method stub
		ComplaintAudit complaintAudit = cad.findComplaintById(oid);
		if(complaintAudit != null){
			complaintAudit.setStatus(status);
			if(status == 3){
//				new MerchantAuditManagerImpl().auditMerchant(complaintAudit.getmId(), 3);
				MerchantAudit merchantAudit = mad.findMerchantAuditById(complaintAudit.getmId());
				if(merchantAudit != null){
					merchantAudit.setStatus(3);
					mad.updateMerchantAudit(merchantAudit);
				}
			}
			return cad.updateComplaintStatus(complaintAudit);
		}else {
			return 0 + "";
		}
	}

	@Override
	public Map<String, Integer> listClientComplaint(String cId) {
		// TODO Auto-generated method stub
		List<ComplaintAudit> complaintAudits = cad.listComplaintByCId(cId);
		Map<String, Integer> map = new HashMap<String, Integer>();
		for (ComplaintAudit complaintAudit : complaintAudits) {
			map.put(complaintAudit.getoId(), complaintAudit.getStatus());
		}
		return map;
	}

	@Override
	public Map<String, Integer> listMerchantComplaint(String mId) {
		// TODO Auto-generated method stub
		List<ComplaintAudit> complaintAudits = cad.listComplaintByMId(mId);
		Map<String, Integer> map = new HashMap<String, Integer>();
		for (ComplaintAudit complaintAudit : complaintAudits) {
			map.put(complaintAudit.getoId(), complaintAudit.getStatus());
		}
		return map;
	}

}
